/*
 * @Author: lin_zizhuo 2297304135@qq.com
 * @Date: 2023-11-24 09:29:46
 * @LastEditors: lin_zizhuo 2297304135@qq.com
 * @LastEditTime: 2023-11-25 07:17:51
 * @FilePath: /mmap_text/text_mmap.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
const size_t map_size = 4096;
int main()
{
    // 如果你打开了一个文件，没有办法做到只让你写不让你读
    // 所以如果想要写入，必须以读写的形式打开
    int fd = open("./log.txt", O_RDWR);
    cout << fd << endl;
    char* area = (char*)mmap(nullptr, map_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
    if(area == (char*)-1)
    {
        cout << "map fail!\n";
        munmap((void*)area, map_size);
        close(fd);
        exit(1);
    }
    // printf("%s", area);
    fgets(area, 4096, stdin);
    int n = munmap((void*)area, map_size);
    close(fd);
    
    return 0;
}